//  /*******************************************************************************
//  * Author      :  ChuanQin Yu                                                   *
//  * Copyright   :  (c) ChuanQin Yu 2023-2024                                     *
//  * mail        :  ycq1014@gmail.com                                             *
//  * License     :  http://www.boost.org/LICENSE_1_0.txt                          *
//  ********************************************************************************

#include "titleBar.h"

TitleBar::TitleBar(QWidget *parent) :
    QWidget(parent),
    parentWidget(parent)
{
    init();

    // 信号槽
    connect(closeButton, &QPushButton::clicked, this, &TitleBar::closeWindowClicked);
    connect(minimizeButton, &QPushButton::clicked, this, &TitleBar::minimizeClicked);
    connect(maximizeButton, &QPushButton::clicked, this, &TitleBar::maximizeClicked);
    connect(fixedButton, &QPushButton::clicked, this, &TitleBar::fixedWindowClicked);
}

TitleBar::~TitleBar()
{

}

void TitleBar::init()
{
    setMinimumSize(QSize(300, 50));

    // 关闭窗口边界
    setWindowFlags(Qt::FramelessWindowHint);

    // 设置窗口颜色为灰色
    QPalette pal = palette();
    pal.setColor(QPalette::Window, QColor(236, 236, 236));
    setAutoFillBackground(true);
    setPalette(pal);

    // 创建一个水平弹簧
    auto *spacer = new QSpacerItem(btnLabelWidth, btnLabelWidth, QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 初始化布局
    layoutBtn = new QHBoxLayout;
    layoutLabel = new QHBoxLayout;
    layout = new QHBoxLayout;

    // 初始化标题栏
    initLabel();
    // 初始化按钮
    initButton();
    layout->addLayout(layoutLabel);
    layout->addItem(spacer);
    layout->addLayout(layoutBtn);

    // 设置布局
    this->setLayout(layout);
}

void TitleBar::initLabel()
{
    // 创建文本水平布局
    layoutLabel = new QHBoxLayout;

    iconLabel = new QLabel(this);
    // 设置图标大小
    iconLabel->setFixedSize(LabelBtnSize);
    // 设置图标
    iconLabel->setPixmap(QPixmap(":/res/application.svg").scaled(iconSize, iconSize));

    titleLabel = new QLabel(this);
    // 设置标题字体
    titleLabel->setFont(QFont("Microsoft Yahei", 10));
    setTitle("Test Title");

    // 创建弹簧
    // title显示在左边，所以这里创建一个弹簧，使得title显示在左边
    auto *spacer = new QSpacerItem(btnLabelWidth, btnLabelWidth, QSizePolicy::Fixed, QSizePolicy::Fixed);

    // title显示在中间, 使用这个弹簧，可以使得title显示在中间
    // auto *spacer = new QSpacerItem(btnLabelWidth, btnLabelWidth, QSizePolicy::Expanding, QSizePolicy::Fixed);

    layoutLabel->addWidget(iconLabel);
    layoutLabel->addItem(spacer);
    layoutLabel->addWidget(titleLabel);
}

void TitleBar::initButton()
{
    // 创建按钮水平布局
    layoutBtn = new QHBoxLayout;

    fixedButton = new QToolButton;
    // 设置按钮大小
    fixedButton->setFixedSize(LabelBtnSize);
    // 设置固定按钮的图标
    fixedButton->setIcon(QIcon(":/res/guding.svg").pixmap(iconSize, iconSize));


    minimizeButton = new QToolButton;
    // 设置按钮大小
    minimizeButton->setFixedSize(LabelBtnSize);
    // 设置最小化按钮的图标
    minimizeButton->setIcon(QIcon(":/res/zuixiaohua.svg").pixmap(iconSize, iconSize));


    maximizeButton = new QToolButton;
    // 设置按钮大小
    maximizeButton->setFixedSize(LabelBtnSize);
    // 设置最大化按钮的图标
    maximizeButton->setIcon(QIcon(":/res/quanping.svg").pixmap(iconSize, iconSize));


    closeButton = new QToolButton(this);
    // 设置按钮大小
    closeButton->setFixedSize(LabelBtnSize);
    // 设置关闭按钮的图标
    closeButton->setIcon(QIcon(":/res/guanbi.svg").pixmap(iconSize, iconSize));
    // 设置按钮的样式表
    closeButton->setStyleSheet("QToolButton:hover {"
                               "  border-radius: 4px;"
                               "  border: 1px solid rgba(252, 53, 3, 50%);"
                               "  background-color: rgba(252, 53, 3, 10%);"
                               "}"
                               "QToolButton:pressed {"
                               "  background-color: rgba(252, 53, 3, 100%);"
                               "}");

    auto spacer = new QSpacerItem(btnLabelWidth, btnLabelWidth, QSizePolicy::Fixed, QSizePolicy::Fixed);

    layoutBtn->addWidget(fixedButton);
    layoutBtn->addItem(spacer);
    layoutBtn->addWidget(minimizeButton);
    layoutBtn->addItem(spacer);
    layoutBtn->addWidget(maximizeButton);
    layoutBtn->addItem(spacer);
    layoutBtn->addWidget(closeButton);
}

void TitleBar::setTitle(const QString &title)
{
    if (!title.isEmpty())
    {
        windowTitle = title;
    }
    titleLabel->setText(windowTitle);
}

void TitleBar::setIcon(const QIcon &icon)
{
    if (icon.isNull())
    {
        return;
    }
    iconLabel->setPixmap(icon.pixmap(16, 16));
}

void TitleBar::setIcon(const QString &iconFile)
{
    if (iconFile.isEmpty())
    {
        return;
    }
    iconLabel->setPixmap(QPixmap(iconFile).scaled(16, 16));
}

void TitleBar::minimizeClicked()
{
    emit minimizeWindow();
}

void TitleBar::maximizeClicked()
{
    isMaximized = !isMaximized;

    if (isMaximized)
    {
        maximizeButton->setIcon(QIcon(":/res/chuangkouhua.svg").pixmap(iconSize, iconSize));
        emit maximizeWindow();
    }
    else
    {
        maximizeButton->setIcon(QIcon(":/res/quanping.svg").pixmap(iconSize, iconSize));
        emit restoreWindow();
    }
}

void TitleBar::closeWindowClicked()
{
    emit closeWindow();
}

// 重写窗口事件
void TitleBar::changeEvent(QEvent *event)
{
    if (event->type() == QEvent::WindowStateChange)
    {
        last_width = parentWidget->width();
        qDebug() << "调用";
    }
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        isPressed = true;
        m_startPoint = event->globalPos();

        if (parentWidget)
        {
            m_windowPoint = parentWidget->pos();
        }
    }
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressed && parentWidget)
    {
       if (!isMaximized)
       {
           parentWidget->move(m_windowPoint + event->globalPos() - m_startPoint);
       }
       else {
           float width_ratio = static_cast<float>(event->globalPos().x()) / static_cast<float>(parentWidget->width());
           maximizeClicked();
           //
           auto new_x = static_cast<float>(last_width * width_ratio);
           int new_width = static_cast<int>(new_x);
           int new_height = this->height() / 4;

           m_windowPoint = event->globalPos() - QPoint(new_width, new_height);
       }
    }
}

void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    isPressed = false;
}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        maximizeClicked();
    }
}

void TitleBar::fixedWindowClicked()
{
    isFixed = !isFixed;

    if (isFixed)
    {
        fixedButton->setIcon(QIcon(":/res/relieve-full.svg").pixmap(iconSize, iconSize));
        emit fixedWindow();
    }
    else
    {
        fixedButton->setIcon(QIcon(":/res/guding.svg").pixmap(iconSize, iconSize));
        emit unFixedWindow();
    }
}
// Path: titleBar.cpp